package jp.co.cos.ittrick.channel.controller.admin;

import jp.co.cos.ittrick.channel.model.ChapterModel;
import jp.co.cos.ittrick.channel.service.ChapterService;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;

import com.google.appengine.api.datastore.Text;

public class ChapterEditController extends Controller {

    ChapterService chapterService;

    public ChapterEditController() {
        chapterService = new ChapterService();
    }

    @Override
    public Navigation run() throws Exception {

        String channel = asString("channel");
        String category = asString("category");
        String chapter = asString("chapter");
        ChapterModel chapterModel = chapterService.getChapterByKey(Integer.valueOf(chapter));

        // カスタムタグのエンエスケープ
        String content = chapterModel.getContentString();
        chapterModel.setContent(new Text(content));

        requestScope("chapterModel", chapterModel);
        requestScope("channel", channel);
        requestScope("category", category);
        requestScope("chapter", chapter);

        // キーワード
        String keyWord = null;
        for(String keyWordTmp: chapterModel.getKeyWordList()) {
            if(keyWord == null) {
                keyWord = keyWordTmp;
            }else {
                keyWord = keyWord + "," + keyWordTmp;
            }
        }
        requestScope("keyWord", keyWord);

        return forward("chapterEdit.jsp");
    }
}
